﻿#pragma once

#include <QMainWindow>
#include <Global.h>

namespace Domy {

class MessageBrowserWindow;
class IssuesBrowserWindow;
class FileExplorerWindow;
class WorkspaceExplorerWindow;
class XMLCoreWindow;
class ActionImpl;

/**
 * @brief The MainWindow class
 * @note  主窗体
 */
class MainWindow : public QMainWindow
{
    Q_OBJECT
    friend class ActionImpl;
public:
    explicit MainWindow(QWidget *parent = Q_NULLPTR);
    ~MainWindow();

    /**
     * @brief registCentralWidget
     * @note  注册中心窗体
     * @param widget 窗体
     */
    void registCentralWidget(QWidget *widget);

    /**
     * @brief saveWindow
     * @note  保存窗口配置
     */
    void saveWindow();

    /**
     * @brief restoreWindow
     * @note  恢复窗口配置
     */
    void restoreWindow();

    /**
     * @brief loadWindowsLayout
     * @note  加载窗口布局
     */
    void loadWindowsLayout();

    /**
     * @brief resetWindowsLayout
     * @note  重置窗口布局
     */
    void resetWindowsLayout();

    /**
     * @brief saveWindowsLayout
     * @note  保存窗口布局
     */
    void saveWindowsLayout();

    /**
     * @brief saveAsWindowsLayout
     * @note  另存为窗口布局
     */
    void saveAsWindowsLayout();

protected:
    virtual void closeEvent(QCloseEvent *event);

private slots:
    void slotOnActionTriggered(ActionKey key, bool is_checked);
    void slotOnRecentFileActionTriggered(const QString &file_path);
    void slotOnNewDocumentFilePathChanged(const QString &file_path);
    void slotOnOpenFile(const QString &file_path);

private:
    void initToolBar();

private:
    ActionImpl              *m_impl;

    QStatusBar              *m_status_bar;

    FileExplorerWindow      *m_file_explorer;

    WorkspaceExplorerWindow *m_workspace_explorer;

    MessageBrowserWindow    *m_msg_browser;

    IssuesBrowserWindow     *m_issues_browser;

    XMLCoreWindow           *m_core_window;

    QByteArray               m_original_layout;
};

/**
 * @brief The ActionImpl class
 * @note  action实现类
 */
class ActionImpl
{
    friend class MainWindow;
public:
    ActionImpl(MainWindow *main);
    ~ActionImpl();

protected:
    void newFile();

    void openFile();

    void openFile(const QString &file_path);

    void closeFile();

    void closeAllFiles();

    void clearRecentFiles();

    void save();

    void saveAs();

    void saveAll();

    void doAction(ActionKey key);

    void settings();

private:
    void showSaveResultMessage(FileSavedReturnCode rslt);

private:
    MainWindow    *m_main;
};

} // namespace Domy
